<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoleFn"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              border=""
              :data="roleList"
            >
              <el-table-column
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                width="240"
              />
              <el-table-column
                prop="description"
                label="描述"
              />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="allotFn(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editFn(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delFn(row.id)"
                  >删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              :span="4"
              type="flex"
              justify="end"
              align="middle"
              style="height: 60px;width:100%"
            >
              <!-- 分页组件 -->
              <span>共{{ totalSize }}页</span>
              <el-pagination
                layout="prev,pager,next"
                :total="totalSize"
                @current-change="pageChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              label-width="120px"
              style="margin-top:50px"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyFrom.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyFrom.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  v-model="companyFrom.companyPhone"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyFrom.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyFrom.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
    <allotDialog
      v-if="title==='分配权限'"
      ref="allotDialog"
    />
    <addEdit
      v-else
      ref="dialog"
      :title="title"
      @updateRole="getRoles"
    />
  </div>
</template>
<script>
import { companyInfo } from '@/api/company'
import { roleList, delRole } from '@/api/roles'
import addEdit from './components/add-edit.vue'
import allotDialog from './components/allot-dialog.vue'
export default {
  components: { addEdit, allotDialog },
  data() {
    return {
      roleList: [],
      companyFrom: {
        companyId: '',
        name: '',
        description: ''
      },
      title: '',
      pageSetting: {
        page: 1,
        pagesize: 10
      },
      totalSize: 0
    }
  },
  async created() {
    this.getRoles()
    this.companyFrom = await companyInfo(
      this.$store.state.user.userInfo.companyId
    )
  },
  methods: {
    //   新增角色
    async addRoleFn() {
      this.title = '新增角色'
      this.$refs.dialog.isShowDialog = true
    },
    // 删除角色
    async delFn(id) {
      await this.$confirm('您确定要删除吗？')
      await delRole(id)
      if (this.roleList.length <= 1 && this.pageSetting.page > 1) {
        this.pageSetting.page--
      }
      await this.getRoles()
      this.$message({
        message: '删除成功',
        type: 'success'
      })
    },
    // 分配权限
    allotFn(id) {
      this.title = '分配权限'
      this.$nextTick(async() => {
        await this.$refs.allotDialog.getPermission(id)
        this.$refs.allotDialog.isShowDialog = true
      })
    },
    editFn(id) {
      this.title = '编辑部门'
      this.$refs.dialog.getRolesDetailInfo(id)
      this.$refs.dialog.isShowDialog = true
    },
    // 分页
    async pageChange(val) {
      this.pageSetting.page = val
      await this.getRoles()
    },
    // 发送请求，获取数据
    async getRoles() {
      const { rows, total } = await roleList(this.pageSetting)
      this.roleList = rows
      this.totalSize = total
    }
  }
}
</script>

<style>
</style>
